#!/bin/bash 

# Simpsons is an open tool for hadoop deployment.

# project home page: http://code.google.com/p/simpsons/

# author  Michael Sheen
# e-mail: michaelsheen1896@gmail.com
# weibo:  http://weibo.com/maximsin

# Your donate will help me improve this project!
# Thank you  very much!

# quick start: 
# to run this shell
# first: chmod 755 /filepath/Simpsons_slave.sh
# then:  bash /filepath/depha_slave.sh


# network configuration
echo "########## Simpson notice: network configuration... start ##########"
sleep 2
interfaces=${interfaces:-"/etc/network/interfaces"}
cp $interfaces $interfaces.bck
cat <<INTERFACES >$interfaces
auto lo
iface lo inet loopback
    
auto eth0
iface eth0 inet static
address 192.168.224.131
netmask 255.255.255.0
gateway 192.168.224.2
INTERFACES

ifdown eth0
sleep 3
ifup eth0
sleep 5
echo "########## Simpson Notice: your current network status is changed as follows.########## " 
ifconfig
sleep 10

# hosts configuration
echo "########## Simpson notice: hosts configuration... start########## "
echo "192.168.224.130 hmaster" >> /etc/hosts 
echo "192.168.224.131 hadoopslave1" >> /etc/hosts
sleep 2

# apt sources configuration
echo "########## Simpson notice: apt sources configuration... start ########## "
sleep 2
source_file=${source_file:-"/etc/apt/sources.list"}
cat <<APT >$source_file

deb http://debian.ustc.edu.cn/ubuntu/ precise main restricted
#deb-src http://debian.ustc.edu.cn/ubuntu/ precise main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://debian.ustc.edu.cn/ubuntu/ precise-updates main restricted
deb-src http://debian.ustc.edu.cn/ubuntu/ precise-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://debian.ustc.edu.cn/ubuntu/ precise universe
deb-src http://debian.ustc.edu.cn/ubuntu/ precise universe
deb http://debian.ustc.edu.cn/ubuntu/ precise-updates universe
deb-src http://debian.ustc.edu.cn/ubuntu//ubuntu/ precise-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu 
## team, and may not be under a free licence. Please satisfy yourself as to 
## your rights to use the software. Also, please note that software in 
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://debian.ustc.edu.cn/ubuntu/ precise multiverse
deb-src http://debian.ustc.edu.cn//ubuntu/ precise multiverse
deb http://debian.ustc.edu.cn/ubuntu/ precise-updates multiverse
deb-src http://debian.ustc.edu.cn/ubuntu/ precise-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://debian.ustc.edu.cn/ubuntu/ precise-backports main restricted universe multiverse
deb-src http://debian.ustc.edu.cn/ubuntu/ precise-backports main restricted universe multiverse

#deb http://debian.ustc.edu.cn/ubuntu precise-security main restricted
#deb-src http://debian.ustc.edu.cn/ubuntu precise-security main restricted
#deb http://debian.ustc.edu.cn/ubuntu precise-security universe
#deb-src http://debian.ustc.edu.cn/ubuntu precise-security universe
#deb http://debian.ustc.edu.cn/ubuntu precise-security multiverse
#deb-src http://debian.ustc.edu.cn/ubuntu precise-security multiverse

## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu precise partner
# deb-src http://archive.canonical.com/ubuntu precise partner

## Uncomment the following two lines to add software from Ubuntu's
## 'extras' repository.
## This software is not part of Ubuntu, but is offered by third-party
## developers who want to ship their latest software.
# deb http://extras.ubuntu.com/ubuntu precise main
# deb-src http://extras.ubuntu.com/ubuntu precise main
APT

# apt update
apt-get update

# shutdown firewall
echo "########## Simpson notice : shutdown firewall... ##########"
ufw disable

# SVN 
#apt-get install subversion

# mount mobile harddisk to /mnt/usb
echo "########## Simpson notice : mount mobile harddisk... ##########"
fdisk -l
mkdir /mnt/usb
mount /dev/sdb1 /mnt/usb

# download jdk and hadoop from mobile harddisk
echo "########## Simpson notice : download jdk and hadoop source files from harddisk... ##########"
mkdir /usr/jdk
cp /mnt/usb/jdk.bin /usr/jdk
cp -r /mnt/usb/hadoop /opt/

# install jdk
echo "########## Simpson notice : install jdk... ##########"
echo "jdk installation..."
chmod +x /usr/jdk/jdk.bin
/usr/jdk/jdk.bin
cp -r /root/jdk1.6.0_30/. /usr/jdk/
rm -r /root/jdk1.6.0_30/

# jdk configuration
echo "########## Simpson notice : jdk classpath conf ##########"
echo "export JAVA_HOME=/usr/jdk" >> /etc/profile
echo "export CLASSPATH=\$CLASSPATH:\$JAVA_HOME/lib:\$JAVA_HOME/jre/lib" >> /etc/profile
echo "export PATH=\$JAVA_HOME/bin:\$JAVA_HOME/jre/bin:\$PATH:\$HOME/bin" >> /etc/profile
sleep 3

# start jdk
bash /etc/profile
sleep 3
echo "########## which java: ##########"
which java
echo "########## JAVA_HOME is :  ########## "
echo $JAVA_HOME
sleep 10

# test jdk environment
echo "######### Simpson notice : jdk setup test #########"
touch /root/Test.java
test_file=${test_file:-"/root/Test.java"}
cat <<TEST_FILE >$test_file
public class Test{
    public static void main(String args []) {
          System.out.println("Jdk is set!");
    }
}
TEST_FILE
javac Test.java
java Test
sleep 5
#rm /root/Test.java

# intstall ssh
echo "########## Simpson notice: ssh intallation... start ##########"
mkdir /root/.ssh
chmod 755 /root/.ssh

# hadoop configuration
echo "########## Simpson notice: chmod...start ##########"
# chmod
chmod 755 /opt/hadoop/ -R
sleep 3

echo "######### Simpson notice: hadoop configuration... start ##########"
# hadoop-env.sh
#cp /opt/hadoop/conf/hadoop-env.sh /opt/hadoop/conf/hadoop-env.sh.bck 
sed ' 10 iexport JAVA_HOME=/usr/jdk' -i /opt/hadoop/conf/hadoop-env.sh
sed ' 17 iexport HADOOP_HEAPSIZE=256' -i /opt/hadoop/conf/hadoop-env.sh

# core-site.xml
#cp /opt/hadoop/conf/core-sits.xml /opt/hadoop/conf/core-sits.xml.bck 
sed ' 7 i<property>' -i /opt/hadoop/conf/core-site.xml
sed ' 8 i<name>fs.default.name</name>' -i /opt/hadoop/conf/core-site.xml
sed ' 9 i<value>hdfs://hmaster:9000</value>' -i /opt/hadoop/conf/core-site.xml
sed ' 10 i</property>' -i /opt/hadoop/conf/core-site.xml
sed ' 11 i<property>' -i /opt/hadoop/conf/core-site.xml
sed ' 12 i<name>hadoop.tmp.dir</name>' -i /opt/hadoop/conf/core-site.xml
sed ' 13 i<value>/home/hadoop/hadoop-${user.name}</value>' -i /opt/hadoop/conf/core-site.xml
sed ' 14 i</property>' -i /opt/hadoop/conf/core-site.xml

# hdfs-site.xml
#cp /opt/hadoop/conf/hdfs-sits.xml /opt/hadoop/conf/hdfs-sits.xml.bck 
sed '7 i<property>' -i /opt/hadoop/conf/hdfs-site.xml
sed '8 i<name>dfs.replication</name>' -i /opt/hadoop/conf/hdfs-site.xml
sed '9 i<value>1</value>' -i /opt/hadoop/conf/hdfs-site.xml
sed '10 i</property>' -i /opt/hadoop/conf/hdfs-site.xml

# mapred-site.xml
#cp /opt/hadoop/conf/hdfs-sits.xml /opt/hadoop/conf/hdfs-sits.xml.bck 
sed '7 i<property>' -i /opt/hadoop/conf/mapred-site.xml
sed '8 i<name>mapred.job.tracker</name>' -i /opt/hadoop/conf/mapred-site.xml
sed '9 i<value>hmaster:9001</value>' -i /opt/hadoop/conf/mapred-site.xml
sed '10 i</property>' -i /opt/hadoop/conf/mapred-site.xml

# master
#cp /opt/hadoop/conf/master /opt/hadoop/conf/master.bck 
master_file=${master_file:-"/opt/hadoop/conf/masters"}
cat <<MASTER_FILE >$master_file
hmaster
MASTER_FILE

# slave
#cp /opt/hadoop/conf/master /opt/hadoop/conf/master.bck 
slave_file=${slave_file:-"/opt/hadoop/conf/slaves"}
cat <<SLAVE_FILE >$slave_file
hmaster
hadoopslave1
SLAVE_FILE
sleep 10

echo "\n########## Simpson notice : hadoop installation is done ! ##########"
echo "\n please enjoy it !"







